layui.config({
    base: '../../layui/' //此处路径请自行处理, 可以使用绝对路径
}).extend({
    formSelects: 'formSelects-v4'
}).use(['jquery', 'formSelects', 'form','layer'], function () {
    const form = layui.form
        , layer = layui.layer
        , $ = layui.jquery;

    form.on('submit(save)', function (data) {
        const params = data.field;
        submit($, params);
        return false;
    });

    function submit($, params) {
        $.ajax({
            url: '/admMq',
            data: JSON.stringify(params),
            dataType: "json",
            contentType: "application/json;charset=utf-8",
            type: "POST",
            success: function (result) {
                if (result.code=200){
                    layer.msg(result.desc, {icon: 1, time: 1000}, function () {
                        CloseWin();
                    })
                }else {
                    layer.msg(result.desc, {icon: 1, time: 1000}, function () {
                        CloseWin();
                    })
                }
            }
        })
    }
    $("#close").on('click', function () {
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    });
    //关闭页面
    function CloseWin() {
        parent.location.reload(); // 父页面刷新
        const index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index); //再执行关闭
    }


    $(function () {
        getDocker("")
        getServer("")
    })

    dockerNameInput = function(e){
        const val = $("#dockerName").val();
        getDocker(val)
    }
    let dockerData
    function getDocker(dockerName) {
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/admDocker/getAll?name="+dockerName,//url
            success: function (result) {
                dockerData = result
                $("#dockerId").html("");
                var html = "";
                $("#dockerId").html("<option value=\"\">请选择docker</option>");
                if (result != null) {
                    $.each(result, function (index, item) {
                        html += "<option  value='" + item.id + "'>" + item.dockerName + "</option>";
                    })
                }
                $("#dockerId").append(html);
                form.render('select');
                if (dockerName != null && dockerName !=''){
                    $(".dockerId").find(".layui-form-select").addClass("layui-form-selected")
                }
            },
            error: function () {
                alert("异常！");
            }
        });
    }
    // 监听下拉框改变事件,修改输入框中的内容
    form.on("select(dockerId)",function(data){
        let value=data.value
        let selectObj=dockerData.filter(item=>{
            return item.id==value
        })
        $("#dockerName").val(selectObj[0].dockerName);
    })


    nameInput = function(e){
        const val = $("#groupName").val();
        getServer(val)

    }
    let dropData

    function getServer(ip) {
        $.ajax({
            type: "GET",//方法类型
            dataType: "json",//预期服务器返回的数据类型
            url: "/server/getAllServer?ip="+ip,//url
            success: function (result) {
                dropData = result
                $("#serverId").html("");
                var html = "";
                $("#serverId").html("<option value=\"\">请选择服务器</option>");
                if (result != null) {
                    $.each(result, function (index, item) {
                        html += "<option  value='" + item.id + "'>" + item.ip + "</option>";
                    })
                }
                $("#serverId").append(html);
                form.render('select');
                if (ip != null && ip !=''){
                    $(".serverId").find(".layui-form-select").addClass("layui-form-selected")
                }
            },
            error: function () {
                alert("异常！");
            }
        });
    }

    // 监听下拉框改变事件,修改输入框中的内容
    form.on("select(serverId)",function(data){
        let value=data.value
        let selectObj=dropData.filter(item=>{
            return item.id==value
        })
        $("#groupName").val(selectObj[0].ip);
    })
});

